package com.lab.book.web.common.util;
/**
import com.google.common.collect.Maps;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@SuppressWarnings("WeakerAccess")
@Component
public class JwtHelper {

	@Value("${jwt.secret}")
	private String secret;

	@Value("${jwt.expire-time-in-second}")
	private Long expirationTimeInSecond;

	public Claims getClaimsFromToken(String token) {
		try {
			return Jwts.parser()
				.setSigningKey(this.secret.getBytes())
				.parseClaimsJws(token)
				.getBody();
		} catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException | IllegalArgumentException e) {
			log.error("token解析错误", e);
			throw new IllegalArgumentException("Token invalided.");
		}
	}

	public Date getExpirationDateFromToken(String token) {
		return getClaimsFromToken(token)
			.getExpiration();
	}

	private Boolean isTokenExpired(String token) {
		Date expiration = getExpirationDateFromToken(token);
		return expiration.before(new Date());
	}

	private Date getExpirationTime() {
		return new Date(System.currentTimeMillis() + this.expirationTimeInSecond * 1000);
	}

	public String generateToken(Map<String, Object> claims) {
		Date createdTime = new Date();
		Date expirationTime = this.getExpirationTime();


		byte[] keyBytes = secret.getBytes();
		SecretKey key = Keys.hmacShaKeyFor(keyBytes);

		return Jwts.builder()
			.setClaims(claims)
			.setIssuedAt(createdTime)
			.setExpiration(expirationTime)
			// 你也可以改用你喜欢的算法
			// 支持的算法详见：https://github.com/jwtk/jjwt#features
			.signWith(key, SignatureAlgorithm.HS256)
			.compact();
	}

	public Boolean validateToken(String token) {
		return !isTokenExpired(token);
	}

	public static void main(String[] args) {
		// 1. 初始化
		JwtHelper jwtHelper = new JwtHelper();
		jwtHelper.expirationTimeInSecond = 1209600L;
		jwtHelper.secret = "aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrsssttt";

		// 2.设置用户信息
		HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
		objectObjectHashMap.put("id", "1");
		objectObjectHashMap.put("name", "jazz");
		objectObjectHashMap.put("mobile", "13428998361");

		// 测试1: 生成token
		String token = jwtHelper.generateToken(objectObjectHashMap);
		// 会生成类似该字符串的内容:
		// eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiamF6eiIsIm1vYmlsZSI6IjEzNDI4OTk4MzYxIiwiaWQiOiIxIiwiaWF0IjoxNTg0NjIxMjYyLCJleHAiOjE1ODU4MzA4NjJ9.MQkSaTIRFQhBpUnMQLeOJFhA5J3Umsasfj5DyShtinQ
		System.out.println(token);

		// 将我改成上面生成的token!!!
		String someToken = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiamF6eiIsIm1vYmlsZSI6IjEzNDI4OTk4MzYxIiwiaWQiOiIxIiwiaWF0IjoxNTg0NjIxMjYyLCJleHAiOjE1ODU4MzA4NjJ9.MQkSaTIRFQhBpUnMQLeOJFhA5J3Umsasfj5DyShtinQ";
		// 测试2: 如果能token合法且未过期，返回true
		Boolean validateToken = jwtHelper.validateToken(someToken);
		System.out.println(validateToken);

		// 测试3: 获取用户信息
		Claims claims = jwtHelper.getClaimsFromToken(someToken);
		System.out.println(claims);

		// 将我改成你生成的token的第一段（以.为边界）
		String encodedHeader = "eyJhbGciOiJIUzI1NiJ9";
		// 测试4: 解密Header
		byte[] header = Base64.decodeBase64(encodedHeader.getBytes());
		System.out.println(new String(header));

		// 将我改成你生成的token的第二段（以.为边界）
		String encodedPayload = "eyJuYW1lIjoiamF6eiIsIm1vYmlsZSI6IjEzNDI4OTk4MzYxIiwiaWQiOiIxIiwiaWF0IjoxNTg0NjIxMjYyLCJleHAiOjE1ODU4MzA4NjJ9";
		// 测试5: 解密Payload
		byte[] payload = Base64.decodeBase64(encodedPayload.getBytes());
		System.out.println(new String(payload));

		// 测试6: 这是一个被篡改的token，因此会报异常，说明JWT是安全的
		jwtHelper.validateToken("eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEiLCJpYXQiOjE1NjU1ODk3MzIsImV4cCI6MTU2Njc5OTMzMn0.nDv25ex7XuTlmXgNzGX46LqMZItVFyNHQpmL9UQf-aUx");
	}
}
*/